package com.juncdt.countdown.controller;

import com.juncdt.countdown.util.R;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/bigScreen")
public class BigScreen  {
    int timeLeng = 150*6*6;//倒计时分钟数
    int midTime1 = timeLeng;
    int midTime2 = timeLeng;
    int midTime3 = timeLeng;
    String timeFormat = "01:30:00";//倒计时起点
    String time1 = timeFormat;
    String time2 = timeFormat;
    String time3 = timeFormat;
    int i = 0;
    @GetMapping("/start")
    public R start(){
        if (i!=0){
            return R.ok("已启动，如需重启请使用重置接口");
        }
        i++;
        //方式一
        new Thread(()->{
            while (midTime1 > 0) {
                midTime1--;
                long hh = midTime1 / 60 / 60;
                long mm = midTime1 / 60 % 60;
                long ss = midTime1 % 60;
                String Hh = "0"+hh;
                String Mm = mm<10?"0"+mm:""+mm;
                String Ss = ss<10?"0"+ss:""+ss;
                time1 = Hh+":"+Mm+":"+Ss;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        //方式二
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                if (midTime2>0){
                    midTime2--;
                    long hh = midTime2 / 60 / 60;
                    long mm = midTime2 / 60 % 60;
                    long ss = midTime2 % 60;
                    String Hh = "0"+hh;
                    String Mm = mm<10?"0"+mm:""+mm;
                    String Ss = ss<10?"0"+ss:""+ss;
                    time2 = Hh+":"+Mm+":"+Ss;
                }
            }
        }, 0, 1000);

        //方式三
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
        executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (midTime3>0){
                    midTime3--;
                    long hh = midTime3 / 60 / 60;
                    long mm = midTime3 / 60 % 60;
                    long ss = midTime3 % 60;
                    String Hh = "0"+hh;
                    String Mm = mm<10?"0"+mm:""+mm;
                    String Ss = ss<10?"0"+ss:""+ss;
                    time3 = Hh+":"+Mm+":"+Ss;
                }
            }
        },0,1, TimeUnit.SECONDS);

        return R.ok();
    }

    @GetMapping("/reset")
    public R reset(String time) throws ParseException {
        if (StringUtils.isNotEmpty(time)){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = format.parse(time);
            Integer midTime = timeLeng - (int)((System.currentTimeMillis() - parse.getTime()) / 1000);
            midTime1 = midTime;
            midTime2 = midTime;
            midTime3 = midTime;
            if (midTime<0){
                time1 = "00:00:00";
                time2 = "00:00:00";
                time3 = "00:00:00";
            }
        }else {
            midTime1 = timeLeng;
            midTime2 = timeLeng;
            midTime3 = timeLeng;
            time1 = timeFormat;
            time2 = timeFormat;
            time3 = timeFormat;
        }
        return R.ok(time1);
    }

    @GetMapping("/time1")
    public R returnTime1(){
        return R.ok(time1);
    }
    @GetMapping("/time2")
    public R returnTime2(){
        return R.ok(time2);
    }
    @GetMapping("/time3")
    public R returnTime3(){
        return R.ok(time3);
    }


}
